package com.oliver.picturebackend.manager;

import com.oliver.picturebackend.config.CosClientConfig;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.COSObject;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.model.ciModel.persistence.PicOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * @ClassName CosManager
 * @Description TODO
 * @Author snow
 * @Date 2024/12/13 21:01
 **/

@Component
public class CosManager {

    @Autowired
    private CosClientConfig cosClientConfig;

    @Autowired
    private COSClient cosClient;


    /**
     * 上传对象
     *
     * @param key  唯一键
     * @param file 文件
     */
    public PutObjectResult putObject(String key, File file) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(cosClientConfig.getBucket(), key, file);

        //  使用腾讯云的cos的数据万象解析
        PicOperations picOperations = new PicOperations();
        //  1表示返回原图信息
        picOperations.setIsPicInfo(1);
        putObjectRequest.setPicOperations(picOperations);
        return cosClient.putObject(putObjectRequest);
    }

    /**
     * 下载对象
     *
     * @param key
     * @return
     */
    public COSObject getObject(String key){
        COSObject object = cosClient.getObject(cosClientConfig.getBucket(), key);
        return object;
    }


}
